/*
 * Copyright 2016-2036 the original author or authors.
 * 
 * COMMERCIAL USE OF THIS SOFTWARE WITHOUT WARRANTY IS NOT ALLOWED.
 * Use is subject to license terms! You can distribute a copy of this software
 * to others for free. This software is a non-profit and open-source project.
 * Any contribution to this project will make it better.
 * All rights reserved! Owned by Stephen Liu.
 * 
 */
package com.github.avatar.config.dubbo;

import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.spring.ServiceBean;
import com.github.avatar.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Export services for remote calling.
 * @author Stephen Liu
 * @since 2017/6/27
 */
@Configuration
@EnableConfigurationProperties(value = Provider.class)
public class ExportServiceConfig extends DubboBaseConfig {

    @Autowired
    private Provider provider;

    @Bean
    public ProviderConfig provider() {
        ProviderConfig providerConfig = new ProviderConfig();
        providerConfig.setMonitor(monitor());
        return providerConfig;
    }

    @Bean
    public ProtocolConfig protocol() {
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setPort(provider.getPort());
        protocolConfig.setThreads(provider.getThreads());
        return protocolConfig;
    }


    /**
     * Actual service for exporting.
     * @param helloService
     * @return
     */
    @Bean
    public ServiceBean<HelloService> dubboServiceExport(HelloService helloService) {
        ServiceBean<HelloService> serviceBean = new ServiceBean<>();
        serviceBean.setProxy(provider.getProxy());
        serviceBean.setInterface(HelloService.class);
        serviceBean.setRef(helloService);
        serviceBean.setVersion(provider.getVersion());
        serviceBean.setRetries(provider.getRetries());
        serviceBean.setTimeout(provider.getTimeout());
        return serviceBean;
    }

}
